using B.S.Data.MES.API.Dto.CangKuDTO;
using B.S.Domain.CangKu;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.Formula.Functions;
using B.S.Data.MES.API.Applications.CangKu.Command;

namespace B.S.Data.MES.API.Applications.CangKu.CommandHandlers
{
    public class GetCangKuPagedCommandHandler : IRequestHandler<GetCangKuPagedCommand, APIRequst<APIPageing<List<CangKuDTO>>>>
    {
        private readonly IBaseRepository<CangKuinfo> _cangKuRepository;
        private readonly IBaseRepository<CangTypeinfo> _cangTypeRepository;
        private readonly IBaseRepository<Account> _roleRepository;

        public GetCangKuPagedCommandHandler(
            IBaseRepository<CangKuinfo> cangKuRepository,
            IBaseRepository<CangTypeinfo> cangTypeRepository,
            IBaseRepository<Account> roleRepository)
        {
            _cangKuRepository = cangKuRepository;
            _cangTypeRepository = cangTypeRepository;
            _roleRepository = roleRepository;
        }

        public  Task<APIRequst<APIPageing<List<CangKuDTO>>>> Handle(GetCangKuPagedCommand request, CancellationToken cancellationToken)
        {
            var query = from ck in _cangKuRepository.GetAll()
                        join ct in _cangTypeRepository.GetAll() on ck.CangTypeId equals ct.CangTypeId
                        join r in _roleRepository.GetAll() on ck.Role equals r.UserNickname
                        select new CangKuDTO 
                        {
                            CangId = ck.CangId,
                            CangName = ck.CangName,
                            CangTypeId = ck.CangTypeId,
                            CangkuNo = ck.CangkuNo,
                            Address = ck.Address,
                            MShu = ck.MShu,
                            Role = r.UserNickname,
                            CangTypeName = ct.CangTypeName,
                            CreateName = ck.CreateName,
                            CreateTime = ck.CreateTime,
                            UpdateTime =ck.UpdateTime
                        };
            if (!string.IsNullOrEmpty(request.CangkuNo))
            {
                query = query.Where(ck => ck.CangkuNo.Contains(request.CangkuNo));
            }
            
            if (!string.IsNullOrEmpty(request.CangName))
            {
                query = query.Where(ck => ck.CangName.Contains(request.CangName));
            }
            
            if (request.CangTypeId.HasValue && request.CangTypeId.Value > 0)
            {
                query = query.Where(ck => ck.CangTypeId == request.CangTypeId.Value);
            }
            
            if (!string.IsNullOrEmpty(request.Role))
            {
                query = query.Where(ck => ck.Role.Contains(request.Role));
            }
           
            APIPageing<List<CangKuDTO>> Page = new APIPageing<List<CangKuDTO>>();
            Page.Data = query
                .OrderBy(q => q.CangId)
                .Skip((request.PageIndex - 1) * request.PageSize)
                .Take(request.PageSize).ToList();
           
            Page.TotalCount = query.Count();
            Page.Total = (int)Math.Ceiling(Page.TotalCount * 1.0 / request.PageSize);
            return Task.FromResult(new APIRequst<APIPageing<List<CangKuDTO>>>
            {
                Code = RequstCode.成功,
                Msg=RequstCode.成功.ToString(),
                Data = Page,
            });

        }
    }
}